package com.sun.talk.netty

import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelInboundHandlerAdapter
import io.netty.handler.timeout.IdleState
import io.netty.handler.timeout.IdleStateEvent


class HeartBeatHandler : ChannelInboundHandlerAdapter() {
    override fun userEventTriggered(ctx: ChannelHandlerContext, evt: Any) {
        if(evt is IdleStateEvent){
            when(evt.state()){
                IdleState.READER_IDLE -> println("进入读空闲")
                IdleState.WRITER_IDLE -> println("进入读空闲")
                IdleState.ALL_IDLE -> {
                    println("channel关闭前，user的数量为${TextWebSocketHandler.users.size}")
                    val channel = ctx.channel()
                    channel.close()
                    println("channel关闭后，user的数量为${TextWebSocketHandler.users.size}")
                }
                else -> {}
            }
        }
    }
}